<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用编码" prop="appCode">
        <el-input v-model="queryParams.appCode" placeholder="请输入应用编码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="应用简称" prop="appShort">
        <el-input v-model="queryParams.appShort" placeholder="请输入应用简称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="应用名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入应用名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="适用平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择适用平台" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.APP_MARKT_PLATFORM)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="浏览量" prop="browse">
        <el-input v-model="queryParams.browse" placeholder="请输入浏览量" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否加密" prop="encrypt">
        <el-select v-model="queryParams.encrypt" placeholder="请选择是否加密" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYS_WHETHER_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="/product/editMarket" v-hasPermi="['faas:app-market:create']">
          <el-button type="primary" plain icon="el-icon-plus" size="mini">新增</el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['faas:app-market:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="主键" align="center" prop="id" v-if="show"/>
      <el-table-column label="租户ID" align="center" prop="lesseeId" v-if="show"/>
      <el-table-column label="应用编码" align="center" prop="appCode" v-if="show"/>
      <el-table-column label="应用简称" align="center" prop="appShort" >
        <template slot-scope="scope">
          <a @click="marketInfo(scope.row)" style="color:blue;cursor:pointer">{{scope.row.appShort}}</a>
        </template>
      </el-table-column>
      <el-table-column label="应用名称" align="center" prop="name" />
      <el-table-column label="最新版本" align="center" prop="latestSn" />
      <el-table-column label="缩率图" align="center" prop="thumbnail" width="160px">
        <template width="90" slot-scope="scope" >
          <el-carousel height="100px">
            <el-carousel-item v-for="item in (scope.row.thumbnail || '').split(',')" :key="item">
              <img  :src="item" class="image" style="width:80px;height:80px;border:none;">
            </el-carousel-item>
          </el-carousel>
        </template>
      </el-table-column>
      <el-table-column label="商标" align="center" prop="logo">
        <template width="90" slot-scope="scope" >
          <img v-if="scope.row.logo" style="width:80px;height:80px;border:none;" :src="scope.row.logo">
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="visible" :formatter="visibleFormat"/>
      <el-table-column label="应用售价" align="center" prop="price" />
      <el-table-column label="安装数量" align="center" prop="install" />
      <el-table-column label="付费安装" align="center" prop="paidInstall" />
      <el-table-column label="适用平台">
        <template #default="{ row }">
          <el-tag style="margin-left: 5px" size="small" v-for="i in row.platform" :key="i">{{getDictDataLabel(DICT_TYPE.APP_MARKT_PLATFORM, i ) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="browse" />
      <el-table-column label="是否加密" align="center" prop="encrypt" :formatter="encryptFormat"/>
      <el-table-column label="应用状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleRenew(scope.row)"
                     v-hasPermi="['faas:app-market:update']">更新</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['faas:app-market:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['faas:app-market:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </div>
</template>

<script>
import { createApp, updateApp, deleteApp, getApp, getAppPage, exportAppExcel } from "@/api/product/market";
import {isShow} from "@/utils/constants";
import {DICT_TYPE, getDictDataLabel} from "@/utils/dict";

export default {
  name: "MgAppMarket",
  components: {
  },
  data() {
    return {
      show:isShow,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用市场列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        lesseeId: null,
        appCode: null,
        appShort: null,
        name: null,
        thumbnail: null,
        logo: null,
        visible: null,
        price: null,
        install: null,
        paidInstall: null,
        platform: null,
        browse: null,
        encrypt: null,
        secrecy: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lesseeId: [{ required: true, message: "租户ID不能为空", trigger: "blur" }],
        appCode: [{ required: true, message: "应用编码不能为空", trigger: "blur" }],
        appShort: [{ required: true, message: "应用简称不能为空", trigger: "blur" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        thumbnail: [{ required: true, message: "缩率图不能为空", trigger: "blur" }],
        visible: [{ required: true, message: "是否显示：0显示 1隐藏不能为空", trigger: "blur" }],
        install: [{ required: true, message: "安装数量不能为空", trigger: "blur" }],
        paidInstall: [{ required: true, message: "付费安装不能为空", trigger: "blur" }],
        platform: [{ required: true, message: "适用平台不能为空", trigger: "blur" }],
        browse: [{ required: true, message: "浏览量不能为空", trigger: "blur" }],
        encrypt: [{ required: true, message: "是否加密：0加密 1未加密不能为空", trigger: "blur" }],
        status: [{ required: true, message: "部门状态（0正常 1停用）不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getAppPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id;
      this.$router.push({
        path: '/product/editMarket',
        query: {
          id: id
        }
      });
    },
    handleRenew(row){
      const id = row.id;
      this.$router.push({
        path: '/product/renew',
        query: {
          id: id
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除应用市场编号为"' + id + '"的数据项?').then(function() {
        return deleteApp(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有应用市场数据项?').then(() => {
        this.exportLoading = true;
        return exportAppExcel(params);
      }).then(response => {
        this.$download.excel(response, '应用市场.xls');
        this.exportLoading = false;
      }).catch(() => {});
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return getDictDataLabel(DICT_TYPE.SYS_COMMON_STATUS, row.status)
    },
    encryptFormat(row, column) {
      return getDictDataLabel(DICT_TYPE.APP_MARKT_ENCRYPT, row.encrypt)
    },
    visibleFormat(row, column) {
      return getDictDataLabel(DICT_TYPE.SYS_COMMON_STATUS, row.visible)
    },
    marketInfo(row){
      this.$router.push({path:'/product/info', query: {id: row.id }})
    },
  }
};
</script>
